home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0063_XMS Library.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  29KB  |  890 lines

  1.  
  2. Unit XMSLib;
  3. { XMSLIB V2.02  Copyright (c) 1994 by Andrew Eigus Fido Net: 2:5100/33 }
  4. { XMS Interface for Turbo Pascal version 7.0 }
  5.  
  6. (*
  7.   XMS termines:
  8.  
  9.   XMS: eXtended Memory Specification
  10.   XMS gives access to extended memory and noncontiguous/nonEMS
  11.       memory above 640K
  12.   UMB: Upper Memory Block
  13.   HMA: High Memory Area
  14.  
  15.   Material used:
  16.  
  17.   C and ASM source of XMS Library (c) by Michael Graff,
  18.   eXtended Memory Specification unit source (c) by Yuval Tal,
  19.   Interrupt List V1.02 (WindowBook) (c) 1984-90 Box Company, Inc.
  20. *)
  21.  
  22. interface
  23.  
  24. const
  25.  
  26.   { XMS function numbers }
  27.  
  28.   XGetVersion    = $00;
  29.   XRequestHMA    = $01;
  30.   XReleaseHMA    = $02;
  31.   XGlobalE20     = $03;
  32.   XGlobalD20     = $04;
  33.   XLocalE20      = $05;
  34.   XLocalD20      = $06;
  35.   XQuery20       = $07;
  36.   XGetMemSize    = $08;
  37.   XAllocEMB      = $09;
  38.   XFreeEMB       = $0A;
  39.   XMoveEMB       = $0B;
  40.   XLockEMB       = $0C;
  41.   XUnlockEMB     = $0D;
  42.   XGetHandleInfo = $0E;
  43.   XReallocEMB    = $0F;
  44.   XRequestUMB    = $10;
  45.   XReleaseUMB    = $11;
  46.  
  47.   { XMS_GetVersion parameters }
  48.  
  49.   XMS = True;  { Get XMS version }
  50.   XMM = False; { Get XMM version }
  51.  
  52.   { XMS functions return codes }
  53.  
  54.   xmsrOk            = $00; { Function successful }
  55.   xmsrNotInitd      = $01; { XMS driver not initialized by XMS_Setup }
  56.   xmsrBadFunction   = $80; { Function not implemented }
  57.   xmsrVDiskDetected = $81; { VDisk was detected }
  58.   xmsrA20Error      = $82; { An A20 error occurred }
  59.   xmsrDriverError   = $8E; { A general driver error }
  60.   xmsrUnrecError    = $8F; { Unrecoverable driver error }
  61.   xmsrNoHMA         = $90; { HMA does not exist }
  62.   xmsrHMAInUse      = $91; { HMA is already in use }
  63.   xmsrHMAMinError   = $92; { HMAMIN parameter is too large }
  64.   xmsrHMANotAlloc   = $93; { HMA is not allocated }
  65.   xmsrA20Enabled    = $94; { A20 line still enabled }
  66.   xmsrNoMoreMem     = $A0; { All extended memory is allocated }
  67.   xmsrNoMoreHandles = $A1; { All available XMS handles are allocated }
  68.   xmsrBadHandle     = $A2; { Invalid handle }
  69.   xmsrBadSourceH    = $A3; { Source handle is invalid }
  70.   xmsrBadSourceO    = $A4; { Source offset is invalid }
  71.   xmsrBadDestH      = $A5; { Destination handle is invalid }
  72.   xmsrBadDestO      = $A6; { Destination offset is invalid }
  73.   xmsrBadLength     = $A7; { Length (size) is invalid }
  74.   xmsrBadOverlap    = $A8; { Move has an invalid overlap }
  75.   xmsrParityError   = $A9; { Parity error occurred }
  76.   xmsrBlkNotLocked  = $AA; { Block is not locked }
  77.   xmsrBlkLocked     = $AB; { Block is locked }
  78.   xmsrBlkLCOverflow = $AC; { Block lock count overflowed }
  79.   xmsrLockFailed    = $AD; { Lock failed }
  80.   xmsrSmallerUMB    = $B0; { Only a smaller UMB is available }
  81.   xmsrNoUMB         = $B1; { No UMB's are available }
  82.   xmsrBadUMBSegment = $B2; { UMB segment number is invalid }
  83.  
  84. type
  85.   THandle = Word; { Memory block handle type }
  86.  
  87. var
  88.   XMSResult : byte; { Returns the status of the last XMS operation performed }
  89.  
  90.  
  91. function XMS_Setup : boolean;
  92. { This function returns True is the extended memory manager device driver
  93.   is installed in memory and active. True if installed, False if not
  94.   installed. You should call this function first, before any other are
  95.   called so it will setup memory manager for use with your program }
  96.  
  97. function XMS_GetVersion(OfWhat : boolean) : word;
  98. { This function returns eighter the version of the extended memory
  99.   specifications version, or the version of the extended memory manager
  100.   device driver version, depends on what you're using as an OfWhat
  101.   parameter (see XMS_GetVersion parameters in const section of the unit).
  102.   The result's low byte is the major version number, and the high byte is
  103.   the minor version number }
  104.  
  105. function XMS_HMAAvail : boolean;
  106. { This function obtains the status of the high memory area (HMA).
  107.   If the result is true, HMA exists. If the result is False no HMA exists }
  108.  
  109. function XMS_AllocHMA(Size : word) : byte;
  110. { This function allocates high memory area (HMA). Size contains the the
  111.   bytes which are needed. The maximum HMA allocation is 65520 bytes.
  112.   The base address of the HMA is FFFF:0010h. If an application fails
  113.   to release the HMA before it terminates, the HMA becomes unavailable
  114.   to the other programs until the system is restarted. Function returns
  115.   zero (xmsrOk) if the call was successful, or one of the xmsr-error codes
  116.   if the call has failed }
  117.  
  118. function XMS_FreeHMA : byte;
  119. { This function releases the high memory area (HMA) and returns zero if
  120.   the call was successful, or one of the xmsr-error codes if the call has
  121.   failed }
  122.  
  123. function XMS_GlobalEnableA20 : byte;
  124. { This function enables the A20 line and should only be used by programs
  125.   that have successfully allocated the HMA. The result is zero if the
  126.   call was successful, otherwise, the result is one of the (xmsr)
  127.   return values }
  128.  
  129. function XMS_GlobalDisableA20 : byte;
  130. { This function disables the A20 line and should only be used by programs
  131.   that do not own the HMA. The result is zero if the call was successful,
  132.   otherwise, the result is one of the (xmsr) return values }
  133.  
  134. function XMS_LocalEnableA20 : byte;
  135. { This function enables the A20 line and should only be used by programs
  136.   that have successfully allocated the HMA. The result is zero if the call
  137.   was successful, otherwise, the result is one of the (xmsr) return values }
  138.  
  139. function XMS_LocalDisableA20 : byte;
  140. { This function disables the A20 line and should only be used by programs
  141.   that do not own the HMA. The A20 line should be disabled before the program
  142.   releases control of the system. The result is zero if the call was
  143.   successful, otherwise, the result is one of the (xmsr) return values }
  144.  
  145. function XMS_QueryA20 : boolean;
  146. { This function returns the status of the A20 address line. If the result is
  147.   True then the A20 line is enabled. If False, it is disabled }
  148.  
  149. function XMS_MemAvail : word;
  150. { This function returns the total free extended memory in kilo-bytes }
  151.  
  152. function XMS_MaxAvail : word;
  153. { This function returns the largest free extended memory block in kilo-bytes }
  154.  
  155. function XMS_AllocEMB(Size : word) : THandle;
  156. { This function allocates extended memory block (EMB). Size defines the size
  157.   of the requested block in kilo-bytes. Function returns a handle number
  158.   which is used by the other EMB commands to refer to this block. If the call
  159.   to this function was unsuccessful, zero is returned instead of the handle
  160.   number and (xmsr) error code is stored in XMSResult variable }
  161.  
  162. function XMS_ReallocEMB(Handle : THandle; Size : word) : byte;
  163. { This function reallocates EMB. Handle is a handle number which was given
  164.   by XMS_AllocEMB. Size defines a new size of the requested block in
  165.   kilo-bytes. Function returns zero if the call was successful, or
  166.   a (xmsr) error code if it failed }
  167.  
  168. function XMS_FreeEMB(Handle : THandle) : byte;
  169. { This function releases allocated extended memory. Handle is a handle number
  170.   which was given by XMS_AllocEMB. Note: If a program fails to release its
  171.   extended memory before it terminates, the memory becomes unavailable to
  172.   other programs until the system is restarted. Blocks may not be released
  173.   while they are locked. Function returns zero if the call was successful, or
  174.   a (xmsr) error code if the call has failed }
  175.  
  176. function XMS_MoveFromEMB(Handle : THandle; var Dest; Count : longint) : byte;
  177. { This function moves data from the extended memory to the conventional
  178.   memory. Handle is a handle number given by XMS_AllocEMB. Dest is a non-typed
  179.   variable so any kind of data can be written there. Count is the number of
  180.   bytes which should be moved. The state of the A20 line is preserved.
  181.   Function returns zero if the call was successful, or a (xmsr) error code
  182.   if the call has failed }
  183.  
  184. function XMS_MoveToEMB(Handle : THandle; var Source; Count : longint) : byte;
  185. { This function moves data from the conventional memory to the extended
  186.   memory. Handle is a handle number given by XMS_AllocEMB. Source is a
  187.   non-typed variable so any kind of data can be written there. Count is
  188.   the number of bytes which should be moved. The state of the A20 line is
  189.   preserved. Function returns zero if the call was successful, or a
  190.   (xmsr) error code if the call has failed }
  191.  
  192. function XMS_LockEMB(Handle : THandle) : pointer;
  193. { This function locks a specified EMB. This function is intended for use by
  194.   programs which enable the A20 line and access extended memory directly.
  195.   Handle is a handle number given by XMS_AllocEMB. The result is a 32-bit
  196.   linear address of the locked block or NIL if lock did not succeed. The
  197.   result value is stored in XMSResult variable }
  198.  
  199. function XMS_UnlockEMB(Handle : THandle) : byte;
  200. { This function unlocks previously locked blocks (by XMS_LockEMB). After
  201.   the EMB is unlocked the 32-bit pointer returned by XMS_LockEMB becomes
  202.   invalid and should not be used. Handle is a handle number given by
  203.   XMS_AllocEMB. The result value is zero if the call was successful,
  204.   otherwise it is one of the (xmsr) return codes }
  205.  
  206. function XMS_EMBHandlesAvail(Handle : THandle) : byte;
  207. { This function returns the number of free handles which are available to
  208.   your program. Handle is a handle number given by XMS_AllocEMB. The result
  209.   value is stored in XMSResult variable }
  210.  
  211. function XMS_EMBLockCount(Handle : THandle) : byte;
  212. { This function returns the lock count of a specified EMB. Handle is a handle
  213.   number given by XMS_AllocEMB. If the function returns zero it means that
  214.   the block is not locked. The result value is stored in XMSResult variable }
  215.  
  216. function XMS_EMBSize(Handle : THandle) : word;
  217. { This function determines the size of a specified EMB. Handle is a handle
  218.   number given by XMS_AllocEMB. The result is the size of the block in
  219.   kilo-bytes. The result code is stored in XMSResult variable }
  220.  
  221. function XMS_AllocUMB(Size : word) : longint;
  222. { This function allocates upper memory blocks (UMBs). Size is the size of
  223.   the block in paragraphs.
  224.   Function returns:
  225.     - segment base of the allocated block in the low-order word
  226.     - actual block size in paragraphs in the high-order word
  227.       In case of an error the high-order word will be the size of the largest
  228.       available block in paragraphs.
  229.   The result code is stored in XMSResult variable }
  230.  
  231. function XMS_FreeUMB(Segment : word) : byte;
  232. { This function releases the memory that was allocated by XMS_FreeUMB.
  233.   Segment must contain the segment base of the block which must be
  234.   released. The result value is zero if the call was successful, or
  235.   one of the (xmsr) error codes, otherwise }
  236.  
  237. function XMS_GetErrorMsg(ErrorCode : byte) : string;
  238. { This function translates the error code which is returned by all the
  239.   XMS_ functions in the unit from a number to a string. The error code is
  240.   written to the global variable XMSResult (byte). If XMSResult is equal
  241.   to zero then no errors were encountered. For more information about
  242.   the result codes, see (xmsr) constants in the unit's const section }
  243.  
  244.  
  245. implementation
  246.  
  247. type
  248.   TransferRec = record
  249.     TransferSize : longint;
  250.     SourceHandle : THandle;
  251.     SourceOffset : longint;
  252.     DestHandle : THandle;
  253.     DestOffset : longint
  254.   end;
  255.  
  256. var
  257.   XMSInitd : boolean;
  258.   XMSDriver : procedure;
  259.   TR : TransferRec; { Internal transfer EMB structure }
  260.  
  261. Function XMS_Setup; assembler;
  262. Asm
  263.   MOV [XMSInitd],False
  264.   MOV AX,4300h        { XMS Driver installation check }
  265.   INT 2Fh
  266.   CMP AL,80h
  267.   JE  @@1             { XMS found }
  268.   MOV AL,False        { else XMS manager not found }
  269.   JMP @@2
  270. @@1:
  271.   MOV AX,4310h        { Get address of XMS driver }
  272.   INT 2Fh
  273.   MOV WORD [XMSDriver],BX    { store offset }
  274.   MOV WORD [XMSDriver+2],ES  { store segment }
  275.   INC [XMSInitd]             { we have init'd our code }
  276.   MOV AL,True
  277. @@2:
  278. End; { XMS_Setup }
  279.  
  280. Function XMS_GetVersion; assembler;
  281. Asm
  282.   MOV [XMSResult],xmsrNotInitd
  283.   CMP [XMSInitd],True
  284.   JNE @@1
  285.   MOV AH,XGetVersion     { Function to get version }
  286.   CALL [XMSDriver]       { Call the XMS driver }
  287.   MOV [XMSResult],xmsrOk
  288.   CMP OfWhat,XMS         { XMS or XMM version? }
  289.   JE  @@1                { If XMS, it's already in AX }
  290.   MOV AX,BX              { If XMM, it's in BX, so move it to AX }
  291. @@1:
  292. End; { XMS_GetVersion }
  293.  
  294. Function XMS_HMAAvail; assembler;
  295. Asm
  296.   MOV [XMSResult],xmsrNotInitd
  297.   CMP [XMSInitd],True
  298.   JNE @@1
  299.   MOV AH,XGetVersion     { Function number }
  300.   CALL [XMSDriver]
  301.   MOV [XMSResult],xmsrOk
  302.   MOV AL,DL              { Store result value }
  303. @@1:
  304. End; { XMS_HMAAvail }
  305.  
  306. Function XMS_AllocHMA; assembler;
  307. Asm
  308.   MOV BL,xmsrNotInitd
  309.   CMP [XMSInitd],True
  310.   JNE @@1
  311.   MOV DX,Size         { Ammount of HMA wanted }
  312.   MOV AH,XRequestHMA  { Function to allocate HMA }
  313.   CALL [XMSDriver]    { Call the XMS driver }
  314.   OR  AX,AX
  315.   JZ  @@1
  316.   XOR BL,BL           { No error }
  317. @@1:
  318.   MOV AL,BL           { Store result value }
  319.   MOV [XMSResult],BL  { Save error code }
  320. End; { XMS_AllocHMA }
  321.  
  322. Function XMS_FreeHMA; assembler;
  323. Asm
  324.   MOV BL,xmsrNotInitd
  325.   CMP [XMSInitd],True
  326.   JNE @@1
  327.   MOV AH,XReleaseHMA  { Function to release HMA }
  328.   CALL [XMSDriver]    { Call the XMS driver }
  329.   OR  AX,AX
  330.   JZ  @@1             { If error then jump, else }
  331.   XOR BL,BL           { clear error code }
  332. @@1:
  333.   MOV AL,BL
  334.   MOV [XMSResult],BL  { Get return code in XMSResult }
  335. End; { XMS_FreeHMA }
  336.  
  337. Function XMS_GlobalEnableA20; assembler;
  338. Asm
  339.   MOV BL,xmsrNotInitd
  340.   CMP [XMSInitd],True
  341.   JNE @@1
  342.   MOV AH,XGlobalE20   { Function code }
  343.   CALL [XMSDriver]    { Call the XMS driver }
  344.   OR  AX,AX
  345.   JZ  @@1
  346.   XOR BL,BL           { Return no error }
  347. @@1:
  348.   MOV AL,BL
  349.   MOV [XMSResult],BL  { Store result value }
  350. End; { XMS_GlobalEnableA20 }
  351.  
  352. Function XMS_GlobalDisableA20; assembler;
  353. Asm
  354.   MOV BL,xmsrNotInitd
  355.   CMP [XMSInitd],True
  356.   JNE @@1
  357.   MOV AH,XGlobalD20   { Function code }
  358.   CALL [XMSDriver]    { Call the XMS driver }
  359.   OR  AX,AX
  360.   JZ  @@1
  361.   XOR BL,BL           { Return success }
  362. @@1:
  363.   MOV AL,BL
  364.   MOV [XMSResult],BL  { Store result value }
  365. End; { XMS_GlobalDisableA20 }
  366.  
  367. Function XMS_LocalEnableA20; assembler;
  368. Asm
  369.   MOV BL,xmsrNotInitd
  370.   CMP [XMSInitd],True
  371.   JNE @@1
  372.   MOV AH,XLocalE20    { Function code }
  373.   CALL [XMSDriver]    { Call the XMS driver }
  374.   OR  AX,AX
  375.   JZ  @@1
  376.   XOR BL,BL           { Return no error value }
  377. @@1:
  378.   MOV AL,BL
  379.   MOV [XMSResult],BL  { Store result value }
  380. End; { XMS_LocalEnableA20 }
  381.  
  382. Function XMS_LocalDisableA20; assembler;
  383. Asm
  384.   MOV BL,xmsrNotInitd
  385.   CMP [XMSInitd],True
  386.   JNE @@1
  387.   MOV AH,XLocalD20    { Function code }
  388.   CALL [XMSDriver]    { Call the XMS driver }
  389.   OR  AX,AX
  390.   JZ  @@1
  391.   XOR BL,BL           { Return no error }
  392. @@1:
  393.   MOV AL,BL
  394.   MOV [XMSResult],BL  { Save result }
  395. End; { XMS_LocalDisableA20 }
  396.  
  397. Function XMS_QueryA20; assembler;
  398. Asm
  399.   MOV BL,xmsrNotInitd
  400.   CMP [XMSInitd],True
  401.   JNE @@1
  402.   MOV AH,XQuery20     { Function code }
  403.   CALL [XMSDriver]    { Call the XMS driver; result in AL }
  404. @@1:
  405.   MOV [XMSResult],BL  { Store error code value }
  406. End; { XMS_QueryA20 }
  407.  
  408. Function XMS_MemAvail; assembler;
  409. Asm
  410.   MOV BL,xmsrNotInitd
  411.   CMP [XMSInitd],True
  412.   JNE @@1
  413.   MOV AH,XGetMemSize  { Function code }
  414.   CALL [XMSDriver]    { Call the XMS driver }
  415.   MOV AX,DX           { AX=Get XMS memory available in K-bytes }
  416. @@1:
  417.   MOV [XMSResult],BL  { Store result value }
  418. End; { XMS_MemAvail }
  419.  
  420. Function XMS_MaxAvail; assembler;
  421. Asm
  422.   MOV BL,xmsrNotInitd
  423.   CMP [XMSInitd],True
  424.   JNE @@1
  425.   MOV AH,XGetMemSize  { Function code }
  426.   CALL [XMSDriver]    { Call the XMS driver }
  427.                       { AX=Get XMS maximum memory block available in K-bytes }
  428. @@1:
  429.   MOV [XMSResult],BL  { Store result value }
  430. End; { XMS_MaxAvail }
  431.  
  432. Function XMS_AllocEMB; assembler;
  433. Asm
  434.   MOV BL,xmsrNotInitd
  435.   CMP [XMSInitd],True
  436.   JNE @@2
  437.   MOV AH,XAllocEMB    { Function code }
  438.   MOV DX,Size         { Number of K-Bytes to allocate }
  439.   CALL [XMSDriver]    { Call the XMS driver }
  440.   OR  AX,AX
  441.   JZ  @@1
  442.   MOV AX,DX           { Store handle number in AX }
  443.   XOR BL,BL           { Set no error }
  444.   JMP @@2
  445. @@1:
  446.   XOR AX,AX           { Return handle 0 if error }
  447. @@2:
  448.   MOV [XMSResult],BL
  449. End; { XMS_AllocEMB }
  450.  
  451. Function XMS_ReallocEMB; assembler;
  452. Asm
  453.   MOV BL,xmsrNotInitd
  454.   CMP [XMSInitd],True
  455.   JNE @@1
  456.   MOV AH,XReallocEMB  { Function code }
  457.   MOV DX,Handle       { Handle number }
  458.   MOV BX,Size         { New size wanted in K-Bytes }
  459.   CALL [XMSDriver]    { Call the XMS driver }
  460.   OR  AX,AX
  461.   JZ  @@1
  462.   XOR BL,BL           { There's no error }
  463. @@1:
  464.   MOV AL,BL           { Return result value }
  465.   MOV [XMSResult],BL  { Store error code }
  466. End; { XMS_ReallocEMB }
  467.  
  468. Function XMS_FreeEMB; assembler;
  469. Asm
  470.   MOV BL,xmsrNotInitd
  471.   CMP [XMSInitd],True
  472.   JNE @@1
  473.   MOV AH,XFreeEMB     { Function code }
  474.   MOV DX,Handle       { Set handle number in DX }
  475.   CALL [XMSDriver]    { Call the XMS driver }
  476.   OR  AX,AX
  477.   JZ  @@1
  478.   XOR BL,BL           { No error }
  479. @@1:
  480.   MOV AL,BL           { Return result value }
  481.   MOV [XMSResult],BL  { Store error code }
  482. End; { XMS_FreeEMB }
  483.  
  484. Function XMS_MoveFromEMB; assembler;
  485. Asm
  486.   PUSH DS
  487.   MOV BL,xmsrNotInitd
  488.   CMP [XMSInitd],True
  489.   JNE @@1
  490.   MOV CX,WORD PTR [Count]
  491.   MOV TR.WORD PTR [TransferSize],CX
  492.   MOV CX,WORD PTR [Count+2]
  493.   MOV TR.WORD PTR [TransferSize+2],CX
  494.   MOV CX,Handle
  495.   MOV TR.SourceHandle,CX
  496.   MOV WORD PTR [TR.SourceOffset],0
  497.   MOV WORD PTR [TR.SourceOffset+2],0
  498.   MOV TR.DestHandle,0
  499.   LES SI,Dest
  500.   MOV WORD PTR [TR.DestOffset],SI
  501.   MOV WORD PTR [TR.DestOffset+2],ES
  502.   MOV AH,XMoveEMB
  503.   MOV DX,SEG TR
  504.   MOV DS,DX
  505.   MOV SI,OFFSET TR
  506.   CALL [XMSDriver]
  507.   OR  AX,AX
  508.   JZ  @@1
  509.   XOR BL,BL
  510. @@1:
  511.   MOV AL,BL
  512.   MOV [XMSResult],BL
  513.   POP DS
  514. End; { XMS_MoveFromEMB }
  515.  
  516. Function XMS_MoveToEMB; assembler;
  517. Asm
  518.   PUSH DS
  519.   MOV BL,xmsrNotInitd
  520.   CMP [XMSInitd],True
  521.   JNE @@1
  522.   MOV CX,WORD PTR [Count]
  523.   MOV TR.WORD PTR [TransferSize],CX
  524.   MOV CX,WORD PTR [Count+2]
  525.   MOV TR.WORD PTR [TransferSize+2],CX
  526.   MOV TR.SourceHandle,0
  527.   LES SI,Source
  528.   MOV WORD PTR [TR.SourceOffset],SI
  529.   MOV WORD PTR [TR.SourceOffset+2],ES
  530.   MOV CX,Handle
  531.   MOV TR.DestHandle,CX
  532.   MOV WORD PTR [TR.DestOffset],0
  533.   MOV WORD PTR [TR.DestOffset+2],0
  534.   MOV AH,XMoveEMB
  535.   MOV DX,SEG TR
  536.   MOV DS,DX
  537.   MOV SI,OFFSET TR
  538.   CALL [XMSDriver]
  539.   OR  AX,AX
  540.   JZ  @@1
  541.   XOR BL,BL
  542. @@1:
  543.   MOV AL,BL
  544.   MOV [XMSResult],BL
  545.   POP DS
  546. End; { XMS_MoveToEMB }
  547.  
  548. Function XMS_LockEMB; assembler;
  549. Asm
  550.   CMP [XMSInitd],True
  551.   JNE @@1             { if not initialized, return the NIL pointer }
  552.   MOV AH,XLockEMB     { Function code }
  553.   MOV DX,Handle       { Handle in DX }
  554.   CALL [XMSDriver]    { Call the XMS driver }
  555.   OR  AX,AX           { Was the call successful? }
  556.   JNZ @@2             { Yep, so jump and return pointer }
  557. @@1:
  558.   XOR AX,AX
  559.   XOR DX,DX           { Return NIL }
  560.   MOV [XMSResult],xmsrLockFailed
  561.   JMP @@3
  562. @@2:
  563.   MOV AX,BX           { Offset in AX, Segment in DX }
  564.   MOV XMSResult,xmsrOk
  565. @@3:
  566. End; { XMS_LockEMB }
  567.  
  568. Function XMS_UnlockEMB; assembler;
  569. Asm
  570.   MOV BL,xmsrNotInitd
  571.   CMP [XMSInitd],True
  572.   JNE @@1
  573.   MOV AH,XUnlockEMB   { Function code }
  574.   MOV DX,Handle       { Handle in DX }
  575.   CALL [XMSDriver]    { Call the XMS driver }
  576.   OR  AX,AX
  577.   JZ  @@1
  578.   XOR BL,BL
  579. @@1:
  580.   MOV AL,BL
  581.   MOV [XMSResult],BL
  582. End; { XMS_UnlockEMB }
  583.  
  584. Function XMS_EMBHandlesAvail; assembler;
  585. Asm
  586.   MOV BL,xmsrNotInitd
  587.   CMP [XMSInitd],True
  588.   JNE @@1
  589.   MOV AH,XGetHandleInfo { Function code }
  590.   MOV DX,Handle
  591.   CALL [XMSDriver]
  592.   OR  AX,AX
  593.   JZ  @@1
  594.   MOV AL,BL             { Save number of free handles }
  595.   XOR BL,BL
  596. @@1:
  597.   MOV [XMSResult],BL
  598. End; { XMS_EMBHandlesAvail }
  599.  
  600. Function XMS_EMBLockCount; assembler;
  601. Asm
  602.   MOV BL,xmsrNotInitd
  603.   CMP [XMSInitd],True
  604.   JNE @@1
  605.   MOV AH,XGetHandleInfo
  606.   MOV DX,Handle         { Handle in DX }
  607.   CALL [XMSDriver]
  608.   OR  AX,AX
  609.   JZ  @@1
  610.   MOV AL,BH             { Save lock count }
  611.   XOR BL,BL
  612. @@1:
  613.   MOV [XMSResult],BL
  614. End; { XMS_EMBLockCount }
  615.  
  616. Function XMS_EMBSize; assembler;
  617. Asm
  618.   MOV BL,xmsrNotInitd
  619.   CMP [XMSInitd],True
  620.   JNE @@1
  621.   MOV AH,XGetHandleInfo
  622.   MOV DX,Handle
  623.   CALL [XMSDriver]
  624.   OR  AX,AX
  625.   JZ  @@1
  626.   MOV AX,DX             { Save EMB size in K-bytes }
  627.   XOR BL,BL
  628. @@1:
  629.   MOV [XMSResult],BL
  630. End; { XMS_EMBSize }
  631.  
  632. Function XMS_AllocUMB; assembler;
  633. Asm
  634.   MOV BL,xmsrNotInitd
  635.   CMP [XMSInitd],True
  636.   JNE @@1
  637.   MOV AH,XRequestUMB  { Function code }
  638.   MOV DX,Size         { Number of paragraphs we want }
  639.   CALL [XMSDriver]    { Call the XMS driver }
  640.   OR  AX,AX
  641.   JZ  @@1
  642.   MOV AX,BX           { Return segment of UMB in low-order word }
  643.                       { Actual block size in high-order word }
  644.   XOR BL,BL
  645. @@1:
  646.   MOV [XMSResult],BL
  647. End; { XMS_AllocUMB }
  648.  
  649. Function XMS_FreeUMB; assembler;
  650. Asm
  651.   MOV BL,xmsrNotInitd
  652.   CMP [XMSInitd],True
  653.   JNE @@1
  654.   MOV AH,XReleaseUMB  { Function code }
  655.   MOV DX,Segment      { Segment of UMB to release }
  656.   CALL [XMSDriver]    { Call the XMS driver }
  657.   OR  AX,AX
  658.   JZ  @@1
  659.   XOR BL,BL
  660. @@1:
  661.   MOV AL,BL
  662.   MOV [XMSResult],BL
  663. End; { XMS_FreeUMB }
  664.  
  665. Function XMS_GetErrorMsg;
  666. var S : ^String;
  667. Begin
  668.   New(S);
  669.   case ErrorCode of
  670.     xmsrNotInitd:      S^ := 'XMS driver not initialized';
  671.     xmsrBadFunction:   S^ := 'Function not implemented';
  672.     xmsrVDiskDetected: S^ := 'VDisk has detected';
  673.     xmsrA20Error:      S^ := 'An A20 error occurred';
  674.     xmsrDriverError:   S^ := 'A general driver error';
  675.     xmsrUnrecError:    S^ := 'Unrecoverable driver error';
  676.     xmsrNoHMA:         S^ := 'HMA does not exist';
  677.     xmsrHMAInUse:      S^ := 'HMA is already in use';
  678.     xmsrHMAMinError:   S^ := 'HMAMIN parameter is too large';
  679.     xmsrHMANotAlloc:   S^ := 'HMA is not allocated';
  680.     xmsrA20Enabled:    S^ := 'A20 line still enabled';
  681.     xmsrNoMoreMem:     S^ := 'All extended memory is allocated';
  682.     xmsrNoMoreHandles: S^ := 'All available XMS handles are allocated';
  683.     xmsrBadHandle:     S^ := 'Invalid block handle';
  684.     xmsrBadSourceH:    S^ := 'Block source handle is invalid';
  685.     xmsrBadSourceO:    S^ := 'Block source offset is invalid';
  686.     xmsrBadDestH:      S^ := 'Block destination handle is invalid';
  687.     xmsrBadDestO:      S^ := 'Block destination offset is invalid';
  688.     xmsrBadLength:     S^ := 'Block length is invalid';
  689.     xmsrBadOverlap:    S^ := 'Move operation has an invalid overlap';
  690.     xmsrParityError:   S^ := 'Parity error';
  691.     xmsrBlkNotLocked:  S^ := 'Block is not locked';
  692.     xmsrBlkLocked:     S^ := 'Block is locked';
  693.     xmsrBlkLCOverflow: S^ := 'Block lock count overflowed';
  694.     xmsrLockFailed:    S^ := 'Lock failed';
  695.     xmsrSmallerUMB:    S^ := 'Too large UMB requested';
  696.     xmsrNoUMB:         S^ := 'No UMB''s are available';
  697.     xmsrBadUMBSegment: S^ := 'UMB segment number is invalid';
  698.     else S^ := 'Unknown error'
  699.   end;
  700.   XMS_GetErrorMsg := S^;
  701.   Dispose(S)
  702. End; { XMS_GetErrorMsg }
  703.  
  704. Begin
  705.   { Initialize global variables }
  706.   XMSInitd := False;
  707.   XMSResult := xmsrOk
  708. End. { XMSLib }
  709.  
  710. { ***** XMSDEMO.PAS ***** }
  711.  
  712. Program XMSLibDemo;
  713. { Copyright (c) 1994 by Andrew Eigus              Fido Net: 2:5100/33 }
  714. { XMS Interface V2.02 for Turbo Pascal version 7.0 demonstration program }
  715.  
  716. (*
  717.   Tested on IBM 486 SX 33Mhz with 4MB RAM with the following configuration:
  718.      1)  HIMEM.SYS  (MS-DOS 6.2 XMS memory manager)
  719.      2)  HIMEM.SYS  (MS-DOS 6.2 XMS memory manager)
  720.   EMM386.EXE (MS-DOS 6.2 EMS memory manager)
  721.  
  722.   If any inpredictable errors occur in your system while running this demo,
  723.   please be so kind to inform me:
  724.  
  725.  AndRew's BBS Phone: 003-712-559777 (Riga, Latvia) 24h 2400bps
  726.  Voice Phone:     003-712-553218
  727.  Fido Net:     2:5100/20.12
  728. *)
  729.  
  730. {X+}{$R-}
  731.  
  732. uses XMSLib;
  733.  
  734. type
  735.   TMsg = array[1..14] of Char;
  736.  
  737.   TUMBAllocRec = record
  738.     Size : word;
  739.     SegAddr : word
  740.   end;
  741.  
  742. const
  743.   Message1 : TMsg = 'First message ';
  744.   Message2 : TMsg = 'Second message';
  745.  
  746.   YesNo : array[boolean] of string[3] = ('No', 'Yes');
  747.   A20State : array[boolean] of string[8] = ('Disabled', 'Enabled');
  748.  
  749. var
  750.   Version, Memory, Handle, BlockLength : word;
  751.   Locks, FreeHandles : byte;
  752.   HMAAvailable : boolean;
  753.   Address : pointer;
  754.   UMB : longint;
  755.  
  756. Function Hex(Num : longint; Places : byte) : string;
  757. const HexTab : array[0..15] of Char = '0123456789ABCDEF';
  758. var
  759.   HS : string[8];
  760.   Digit : byte;
  761. Begin
  762.   HS[0] := Chr(Places);
  763.   for Digit := Places downto 1 do
  764.   begin
  765.     HS[Digit] := HexTab[Num and $0000000F];
  766.     Num := Num shr 4
  767.   end;
  768.   Hex := HS
  769. End; { Hex }
  770.  
  771. Function Check(Result : byte; Func : string) : byte;
  772. Begin
  773.   if Result <> xmsrOk then
  774.     WriteLn(Func, ' returned ',
  775.       Hex(Result, 2), 'h (', Result, '): ', XMS_GetErrorMsg(Result));
  776.   Check := Result
  777. End; { Check }
  778.  
  779. Procedure ShowA20State;
  780. var State : boolean;
  781. Begin
  782.   State := XMS_QueryA20;
  783.   if Check(XMSResult, 'XMS_QueryA20') = xmsrOk then
  784.     WriteLn('A20 state: ', A20State[State])
  785. End; { ShowA20State }
  786.  
  787. Procedure Wait4Return;
  788. Begin
  789.   WriteLn;
  790.   WriteLn('Press ENTER to continue');
  791.   ReadLn
  792. end; { Wait4Return }
  793.  
  794.  
  795. Begin
  796.   WriteLn('XMS Library V2.02 Demonstration program by Andrew Eigus'#10);
  797.   if XMS_Setup then
  798.   begin
  799.  
  800.     Version := XMS_GetVersion(XMS);
  801.     if Check(XMSResult, 'XMS_GetVersion(XMS)') = xmsrOk then
  802.       WriteLn('XMS version ', Hi(Version), '.', Lo(Version), ' present');
  803.     Version := XMS_GetVersion(XMM);
  804.     if Check(XMSResult, 'XMS_GetVersion(XMM)') = xmsrOk then
  805.       WriteLn('XMM version ', Hi(Version), '.', Lo(Version), ' detected');
  806.     HMAAvailable := XMS_HMAAvail;
  807.     if Check(XMSResult, 'XMS_HMAAvail') = xmsrOk then
  808.       WriteLn('HMA Available: ', YesNo[HMAAvailable]);
  809.  
  810.     WriteLn;
  811.     Memory := XMS_MemAvail;
  812.     if Check(XMSResult, 'XMS_MemAvail') = xmsrOk then
  813.       WriteLn('Free XMS memory available: ', Memory, ' KB')
  814.     else
  815.       if XMSResult = xmsrNoMoreMem then Halt(xmsrNoMoreMem);
  816.     Memory := XMS_MaxAvail;
  817.     if Check(XMSResult, 'XMS_MaxAvail') = xmsrOk then
  818.       WriteLn('Largest XMS memory block: ', Memory, ' KB');
  819.  
  820.     WriteLn;
  821.     if HMAAvailable then
  822.       if Check(XMS_AllocHMA($FFFF), 'XMS_AllocHMA') = xmsrOk then
  823.       begin
  824.         WriteLn('HMA: Block allocated');
  825.         if Check(XMS_FreeHMA, 'XMS_FreeHMA') = xmsrOk then
  826.           WriteLn('HMA: Block released')
  827.       end;
  828.  
  829.     Wait4Return;
  830.  
  831.     WriteLn('XMS data transfer test'#10);
  832.     WriteLn('Message1: ', Message1);
  833.     WriteLn('Message2: ', Message2);
  834.  
  835.     Handle := XMS_AllocEMB(1);
  836.     if Check(XMSResult, 'XMS_AllocEMB') = xmsrOk then
  837.     begin
  838.       WriteLn('1 KB EMB allocated. Handle number: ', Hex(Handle, 4), 'h');
  839.       { Now copy our little Message1 to extended memory }
  840.       if Check(XMS_MoveToEMB(Handle, Message1, SizeOf(TMsg)),
  841.         'XMS_MoveToEMB') = xmsrOk then WriteLn('Transfer to XMS: OK');
  842.       { Now copy it back to the second string }
  843.       if Check(XMS_MoveFromEMB(Handle, Message2, SizeOf(TMsg)),
  844.         'XMS_MoveFromEMB') = xmsrOk then WriteLn('Transfer from XMS: OK');
  845.       WriteLn('Message1: ', Message1);
  846.       WriteLn('Message2: ', Message2);
  847.       WriteLn;
  848.       if Check(XMS_ReallocEMB(Handle, 2),
  849.         'XMS_ReallocEMB') = xmsrOk then
  850.         WriteLn('EMB reallocated. New size: 2 KB');
  851.       WriteLn;
  852.       Address := XMS_LockEMB(Handle);
  853.       if Check(XMSResult, 'XMS_LockEMB') = xmsrOk then
  854.         WriteLn('EMB locked at linear memory address ',
  855.           Hex(Longint(Address), 8), 'h');
  856.  
  857.       WriteLn;
  858.       FreeHandles := XMS_EMBHandlesAvail(Handle);
  859.       if Check(XMSResult, 'XMS_EMBHandlesAvail') = xmsrOk then
  860.         WriteLn('EMB Handles available: ', FreeHandles);
  861.       Locks := XMS_EMBLockCount(Handle);
  862.       if Check(XMSResult, 'XMS_EMBLockCount') = xmsrOk then
  863.         WriteLn('EMB Lock count: ', Locks);
  864.       BlockLength := XMS_EMBSize(Handle);
  865.       if Check(XMSResult, 'XMS_EMBSize') = xmsrOk then
  866.         WriteLn('EMB Length: ', BlockLength, ' KB');
  867.  
  868.       WriteLn;
  869.       if Check(XMS_UnlockEMB(Handle), 'XMS_UnlockEMB') = xmsrOk then
  870.           WriteLn('EMB unlocked');
  871.  
  872.       WriteLn;
  873.       if Check(XMS_FreeEMB(Handle), 'XMS_FreeEMB') = xmsrOk then
  874.         WriteLn('EMB released');
  875.  
  876.       Wait4Return
  877.     end;
  878.  
  879.     UMB := XMS_AllocUMB($FFFF);
  880.     if Check(XMSResult, 'XMS_AllocUMB') = xmsrOk then
  881.     begin
  882.       WriteLn('UMB allocated at segment base ',
  883.         Hex(TUMBAllocRec(UMB).SegAddr, 4), 'h');
  884.       WriteLn('Actual size: ', TUMBAllocRec(UMB).Size, ' paragraphs'#10);
  885.       if Check(XMS_FreeUMB(TUMBAllocRec(UMB).SegAddr),
  886.         'XMS_FreeUMB') = xmsrOk then WriteLn('UMB released')
  887.     end;
  888.   end else WriteLn('XMS not present.')
  889. End.
  890.